/**
 * Created by cb on 15/3/28.
 */
//interface HandleNumberInterface {
//    update(target: Any): void;
//}
var __extends = this.__extends || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};
//被观察者的基类
var ConcreteSubject = (function () {
    function ConcreteSubject() {
        this.observers = [];
    }
    ConcreteSubject.prototype.addObserver = function (observer) {
        this.observers.push(observer);
    };
    ConcreteSubject.prototype.removeObserver = function (observer) {
        var key = -1;
        if ((key = this.observers.indexOf(observer)) != -1)
            this.observers.splice(key, 1);
    };
    ConcreteSubject.prototype.update = function (targer) {
        this.observers.forEach(function (o) {
            o.update(targer);
        });
    };
    return ConcreteSubject;
})();
var NumberChange = (function (_super) {
    __extends(NumberChange, _super);
    function NumberChange(target) {
        _super.call(this);
        this.config = {
            min: null,
            max: null
        };
        this.bindHandles(target);
    }
    NumberChange.prototype.bindHandles = function (targets) {
        var _this = this;
        targets.each(function (i, o) {
            _this.bindHandle($(o));
        });
    };
    NumberChange.prototype.bindHandle = function (target) {
        var _this = this;
        target.find('.cart-num-plus').bind('click', function () {
            _this.plusNumber(target);
        });
        target.find('.cart-num-minus').bind('click', function () {
            _this.minusNumber(target);
        });
    };
    NumberChange.prototype.plusNumber = function (target) {
        this.changeNumber(target.find('input'), 1);
    };
    NumberChange.prototype.minusNumber = function (target) {
        this.changeNumber(target.find('input'), -1);
    };
    NumberChange.prototype.changeNumber = function (target, nSpec) {
        var number = parseInt(target.val());
        number = isNaN(number) ? Math.abs(nSpec) : this.checnedCycle(number + nSpec) ? number + nSpec : number;
        target.val(number);
        this.update(target);
    };
    NumberChange.prototype.checnedCycle = function (num) {
        return typeof this.config.min == 'number' && typeof this.config.max == 'number' ? (num >= this.config.min && num <= this.config.max) : (typeof this.config.min == 'number' ? num >= this.config.min : (typeof this.config.max == 'number' ? num <= this.config.max : true));
    };
    return NumberChange;
})(ConcreteSubject);
var CartController = (function () {
    function CartController() {
        this.carts = [];
    }
    CartController.prototype.update = function (target) {
    };
    CartController.prototype.delCartItem = function () {
    };
    //计算选中购物车项的总金额
    CartController.prototype.sumCheckedAmount = function () {
    };
    //计算选中的总数量
    CartController.prototype.sumCheckedNumber = function () {
    };
    CartController.prototype.bindEvent = function (target, parent) {
        this[target.attr('method')] && this[target.attr('method')](target, parent);
    };
    CartController.prototype.bindChange = function (target, parent) {
        target.unbind('keydown').bind('keydown', function (e) {
        });
    };
    CartController.prototype.changeNumber = function () {
    };
    CartController.prototype.checkedCartItem = function (target, parent) {
    };
    //删除购物车
    CartController.prototype.delCartItem = function (target, parent) {
        parent.remove();
    };
    //删除购物车项的请求
    CartController.prototype.delCartItemRequest = function () {
        //TODO 异步请求删除
    };
    return CartController;
})();
$(function () {
    var oCart = new CartController();
    $('.cart-list-item').click(function (event) {
        var event = event || window.event, target = event.target || event.srcElement;
        oCart.bindEvent($(target), $(this));
    });
    var a = new NumberChange($('.hp-stock'));
    a.config.min = 1;
    a.addObserver(oCart);
});
